<?php
namespace app\article\controller;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminArticle extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
    }
    
    public function itemAdd()
    {
        $data = $this->request->post();
        if (!isset($data['title']) || !$data['title']) {
            return jsonError('请输入标题');
        }
        $itemInfo = db($this->item)->where('title',$data['title'])->find();
        if ($itemInfo) {
            return jsonError('标题已存在');
        }
        $fieldList = input('post.fieldList');
        $fieldList = json_decode($fieldList,true);
        $res = model($this->itemModelNameSpace)->itemAdd($data,$fieldList);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }
    
    public function itemEdit()
    {
		$data = $this->request->post();
        if (!isset($data['title']) || !$data['title']) {
            return jsonError('请输入标题');
        }
        $itemInfo = db($this->item)->where('uuid',$data['uuid'])->find();
        if (!$itemInfo) {
            return jsonError('不存在');
        }
        $itemInfo = db($this->item)->where('uuid','<>',$data['uuid'])->where('title',$data['title'])->find();
        if ($itemInfo) {
            return jsonError('标题已存在');
        }
        if (!isset($data['content']) || !$data['content']) {
            return jsonError('请输入内容');
        }
        $fieldList = input('post.fieldList');
        $fieldList = json_decode($fieldList,true);
        $res = model($this->itemModelNameSpace)->itemEdit($data,$fieldList);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }
	
    public function itemDel()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db($this->item)->where('id',$id)->find();
        if (!$itemInfo) {
            return jsonError('删除项不存在');
        }
        $res = model($this->itemModelNameSpace)->itemDel($id);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }

    public function itemsDel()
    {
        $ids = input('post.ids');
        if (!$ids) {
          return jsonError('缺少参数');
        }
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            model($this->itemModelNameSpace)->itemDel($id);
        }
        return jsonSuccess('操作成功');
    }
    
    public function itemTransferAll()
    {
        $cid = input('post.cid');
        $ids = input('post.ids');
        if (!$ids) {
          return jsonError('缺少参数');
        }
        if ($cid == '') {
            $cid = 0;
        }
        $ids = explode(',',$ids);
        if (is_array($ids)) {
            foreach ($ids as $id){
                db($this->item)->where('id',$id)->update(['cid' => $cid]);
            }
            return jsonSuccess('操作成功');
        } else {
            return  jsonError('参数错误');
        }
    }
    
    public function itemInfo()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = model($this->itemModelNameSpace)->getItemInfo($id);
        if (!$itemInfo) {
          return jsonError('不存在');
        }
        $itemInfo = model($this->itemModelNameSpace)->getContentByItemInfo($itemInfo);
        return jsonSuccess('',$itemInfo);
    }

    public function itemList()
    {
        $data = $this->request->post();
        $type = $data['type'];
        $where = null;
        if ($type) {
        	//MIP
            if ($type == 'mipOk') {
                $where['mip_push_num'] = 1;
            }
            if ($type == 'mipNo') {
                $where['mip_push_num'] = 0;
            }
            //sm
            if ($type == 'smOk') {
                $where['sm_push_num'] = 1;
            }
            if ($type == 'smNo') {
                $where['sm_push_num'] = 0;
            }
            //link
            if ($type == 'linkOk') {
                $where['link_push_num'] = 1;
            }
            if ($type == 'linkNo') {
                $where['link_push_num'] = 0;
            }
            //xz
            if ($type == 'xzhOk') {
                $where['xzh_push_num'] = 1;
            }
            if ($type == 'xzhNo') {
                $where['xzh_push_num'] = 0;
            }
            //抓取
            if ($type == 'collectOk') {
                $where['baidu_spider_num'] = 1;
            }
            if ($type == 'collectNo') {
                $where['baidu_spider_num'] = 0;
            }
            //小程序推送
            if ($type == 'hourOk') {
                $where['baiduAppHourNum'] = 1;
            }
            if ($type == 'hourNo') {
                $where['baiduAppHourNum'] = 0;
            }
            
            if ($type == 'dayOk') {
                $where['baiduAppDayNum'] = 1;
            }
            if ($type == 'dayNo') {
                $where['baiduAppDayNum'] = 0;
            }
            
            if ($type == 'weekOk') {
                $where['baiduAppWeekNum'] = 1;
            }
            if ($type == 'weekNo') {
                $where['baiduAppWeekNum'] = 0;
            }
            $data['where'] = $where;
        }
        $data['permissionType'] = 'admin';
        $itemList = model($this->itemModelNameSpace)->getItemList($data);
        $itemCount = model($this->itemModelNameSpace)->getCount($data['cid'], $data['keywords']);
        if ($data['domain']) {
            if ($itemList) {
                foreach ($itemList as $key => $val) {
                    $itemList[$key]['url'] = model($this->itemModelNameSpace)->getUrlByItemInfo($val,$data['domain']);
                }
            }
        }
	    return jsonSuccess('',['itemList' => $itemList,'total' => $itemCount,'page' => $data['page']]);
    }
    
    public function itemRecomment()
    {
        $id = input('post.id');
        $itemInfo = db($this->item)->where('id',$id)->find();
        if (!$itemInfo) {
            return jsonError('不存在');
        }
        if ($itemInfo['is_recommend'] == 1) {
            $type = 0;
        } else {
            $type = 1;
        }
        db($this->item)->where('id',$id)->update([
            'is_recommend' => $type,
        ]);
        return  jsonSuccess('操作成功');
    }
    
}